﻿Imports Microsoft.Win32
Imports System
Imports System.Drawing.Printing
Imports System.Windows.Forms

Namespace System.Workflow.ComponentModel.Design
    Friend NotInheritable Class PageSetupData
        ' Methods
        Friend Sub New()
            Dim key As RegistryKey = Registry.CurrentUser.OpenSubKey(PageSetupData.WinOEPrintingSubKey)
            If (Not key Is Nothing) Then
                Try 
                    Dim obj2 As Object = Nothing
                    obj2 = key.GetValue("HeaderAlignment")
                    If ((Not obj2 Is Nothing) AndAlso TypeOf obj2 Is Integer) Then
                        Me.headerAlignment = DirectCast(obj2, HorizontalAlignment)
                    End If
                    obj2 = key.GetValue("FooterAlignment")
                    If ((Not obj2 Is Nothing) AndAlso TypeOf obj2 Is Integer) Then
                        Me.footerAlignment = DirectCast(obj2, HorizontalAlignment)
                    End If
                    obj2 = key.GetValue("HeaderMargin")
                    If ((Not obj2 Is Nothing) AndAlso TypeOf obj2 Is Integer) Then
                        Me.headerMargin = CInt(obj2)
                    End If
                    obj2 = key.GetValue("FooterMargin")
                    If ((Not obj2 Is Nothing) AndAlso TypeOf obj2 Is Integer) Then
                        Me.footerMargin = CInt(obj2)
                    End If
                    obj2 = key.GetValue("HeaderTemplate")
                    If ((Not obj2 Is Nothing) AndAlso TypeOf obj2 Is String) Then
                        Me.headerTemplate = CStr(obj2)
                    End If
                    obj2 = key.GetValue("FooterTemplate")
                    If ((Not obj2 Is Nothing) AndAlso TypeOf obj2 Is String) Then
                        Me.footerTemplate = CStr(obj2)
                    End If
                    obj2 = key.GetValue("HeaderCustom")
                    If ((Not obj2 Is Nothing) AndAlso TypeOf obj2 Is Integer) Then
                        Me.headerCustom = Convert.ToBoolean(CInt(obj2))
                    End If
                    obj2 = key.GetValue("FooterCustom")
                    If ((Not obj2 Is Nothing) AndAlso TypeOf obj2 Is Integer) Then
                        Me.footerCustom = Convert.ToBoolean(CInt(obj2))
                    End If
                    obj2 = key.GetValue("CenterHorizontally")
                    If ((Not obj2 Is Nothing) AndAlso TypeOf obj2 Is Integer) Then
                        Me.centerHorizontally = Convert.ToBoolean(CInt(obj2))
                    End If
                    obj2 = key.GetValue("CenterVertically")
                    If ((Not obj2 Is Nothing) AndAlso TypeOf obj2 Is Integer) Then
                        Me.centerVertically = Convert.ToBoolean(CInt(obj2))
                    End If
                Finally
                    key.Close
                End Try
            End If
            Dim settings As New PrinterSettings
            Me.landscape = settings.DefaultPageSettings.Landscape
            Me.margins = settings.DefaultPageSettings.Margins
        End Sub

        Public Sub StorePropertiesToRegistry()
            Dim key As RegistryKey = Registry.CurrentUser.CreateSubKey(PageSetupData.WinOEPrintingSubKey)
            If (Not key Is Nothing) Then
                Try 
                    key.SetValue("HeaderAlignment", CInt(Me.headerAlignment))
                    key.SetValue("FooterAlignment", CInt(Me.footerAlignment))
                    key.SetValue("HeaderMargin", Me.headerMargin)
                    key.SetValue("FooterMargin", Me.footerMargin)
                    key.SetValue("HeaderTemplate", Me.headerTemplate)
                    key.SetValue("FooterTemplate", Me.footerTemplate)
                    key.SetValue("HeaderCustom", Convert.ToInt32(Me.headerCustom))
                    key.SetValue("FooterCustom", Convert.ToInt32(Me.footerCustom))
                    key.SetValue("CenterHorizontally", Convert.ToInt32(Me.centerHorizontally))
                    key.SetValue("CenterVertically", Convert.ToInt32(Me.centerVertically))
                Finally
                    key.Close
                End Try
            End If
        End Sub


        ' Properties
        Public Property AdjustToScaleFactor As Boolean
            Get
                Return Me.adjustToScaleFactor
            End Get
            Set(ByVal value As Boolean)
                Me.adjustToScaleFactor = value
            End Set
        End Property

        Public Property CenterHorizontally As Boolean
            Get
                Return Me.centerHorizontally
            End Get
            Set(ByVal value As Boolean)
                Me.centerHorizontally = value
            End Set
        End Property

        Public Property CenterVertically As Boolean
            Get
                Return Me.centerVertically
            End Get
            Set(ByVal value As Boolean)
                Me.centerVertically = value
            End Set
        End Property

        Public Property FooterAlignment As HorizontalAlignment
            Get
                Return Me.footerAlignment
            End Get
            Set(ByVal value As HorizontalAlignment)
                Me.footerAlignment = value
            End Set
        End Property

        Public Property FooterCustom As Boolean
            Get
                Return Me.footerCustom
            End Get
            Set(ByVal value As Boolean)
                Me.footerCustom = value
            End Set
        End Property

        Public Property FooterMargin As Integer
            Get
                Return Me.footerMargin
            End Get
            Set(ByVal value As Integer)
                If (value >= 0) Then
                    Me.footerMargin = value
                End If
            End Set
        End Property

        Public Property FooterTemplate As String
            Get
                Return Me.footerTemplate
            End Get
            Set(ByVal value As String)
                Me.footerTemplate = value
            End Set
        End Property

        Public Property HeaderAlignment As HorizontalAlignment
            Get
                Return Me.headerAlignment
            End Get
            Set(ByVal value As HorizontalAlignment)
                Me.headerAlignment = value
            End Set
        End Property

        Public Property HeaderCustom As Boolean
            Get
                Return Me.headerCustom
            End Get
            Set(ByVal value As Boolean)
                Me.headerCustom = value
            End Set
        End Property

        Public Property HeaderMargin As Integer
            Get
                Return Me.headerMargin
            End Get
            Set(ByVal value As Integer)
                If (value >= 0) Then
                    Me.headerMargin = value
                End If
            End Set
        End Property

        Public Property HeaderTemplate As String
            Get
                Return Me.headerTemplate
            End Get
            Set(ByVal value As String)
                Me.headerTemplate = value
            End Set
        End Property

        Public Property Landscape As Boolean
            Get
                Return Me.landscape
            End Get
            Set(ByVal value As Boolean)
                Me.landscape = value
            End Set
        End Property

        Public Property Margins As Margins
            Get
                Return Me.margins
            End Get
            Set(ByVal value As Margins)
                Me.margins = value
            End Set
        End Property

        Public Property PagesTall As Integer
            Get
                If (Me.pagesTall >= 1) Then
                    Return Me.pagesTall
                End If
                Return 1
            End Get
            Set(ByVal value As Integer)
                If (value > 0) Then
                    Me.pagesTall = value
                End If
            End Set
        End Property

        Public Property PagesWide As Integer
            Get
                If (Me.pagesWide >= 1) Then
                    Return Me.pagesWide
                End If
                Return 1
            End Get
            Set(ByVal value As Integer)
                If (value > 0) Then
                    Me.pagesWide = value
                End If
            End Set
        End Property

        Public Property ScaleFactor As Integer
            Get
                Return Me.scaleFactor
            End Get
            Set(ByVal value As Integer)
                If ((value >= PageSetupData.DefaultMinScaleFactor) AndAlso (value <= PageSetupData.DefaultMaxScaleFactor)) Then
                    Me.scaleFactor = value
                End If
            End Set
        End Property


        ' Fields
        Private adjustToScaleFactor As Boolean = True
        Private centerHorizontally As Boolean
        Private centerVertically As Boolean
        Friend Shared ReadOnly DefaultFooterMargin As Integer = 50
        Friend Shared ReadOnly DefaultHeaderMargin As Integer = 50
        Friend Shared ReadOnly DefaultMaxScaleFactor As Integer = 400
        Friend Shared ReadOnly DefaultMinScaleFactor As Integer = 10
        Friend Shared ReadOnly DefaultPages As Integer = 1
        Friend Shared ReadOnly DefaultScaleFactor As Integer = 100
        Private footerAlignment As HorizontalAlignment = HorizontalAlignment.Center
        Private footerCustom As Boolean
        Private footerMargin As Integer = PageSetupData.DefaultFooterMargin
        Private footerTemplate As String = String.Empty
        Private headerAlignment As HorizontalAlignment = HorizontalAlignment.Center
        Private headerCustom As Boolean
        Private headerMargin As Integer = PageSetupData.DefaultHeaderMargin
        Private headerTemplate As String = String.Empty
        Private landscape As Boolean
        Private margins As Margins
        Private pagesTall As Integer = PageSetupData.DefaultPages
        Private pagesWide As Integer = PageSetupData.DefaultPages
        Private Const RegistryCenterHorizontally As String = "CenterHorizontally"
        Private Const RegistryCenterVertically As String = "CenterVertically"
        Private Const RegistryFooterAlignment As String = "FooterAlignment"
        Private Const RegistryFooterCustom As String = "FooterCustom"
        Private Const RegistryFooterMarging As String = "FooterMargin"
        Private Const RegistryFooterTemplate As String = "FooterTemplate"
        Private Const RegistryHeaderAlignment As String = "HeaderAlignment"
        Private Const RegistryHeaderCustom As String = "HeaderCustom"
        Private Const RegistryHeaderMarging As String = "HeaderMargin"
        Private Const RegistryHeaderTemplate As String = "HeaderTemplate"
        Private scaleFactor As Integer = PageSetupData.DefaultScaleFactor
        Private Shared ReadOnly WinOEPrintingSubKey As String = (DesignerHelpers.DesignerPerUserRegistryKey & "\Printing")
    End Class
End Namespace

